export function getGrowAge(birthday, now = new Date()) {
  var time
  try {
    time = birthday
      .replace('年', '-')
      .replace('月', '-')
      .replace('日', '')
  } catch (e) {
    time = this.currentTime
  }
  
  // var now = new Date()
  var year = now.getFullYear()
  var month = now.getMonth() + 1
  var day = now.getDate()
  var hour = now.getHours()
  var minute = now.getMinutes()
  var second = now.getSeconds()

  var myDate = new Date(time)
  var myYear = myDate.getFullYear()
  var myMonth = myDate.getMonth() + 1
  var myDay = myDate.getDate()
  var myHour = myDate.getHours()
  var myMinute = myDate.getMinutes()
  var mySecond = myDate.getSeconds()

  var gapSecond = second - mySecond
  if (gapSecond < 0) {
    minute -= 1
    gapSecond = 60 - mySecond + second
  }
  var gapMinute = minute - myMinute
  if (gapMinute < 0) {
    hour -= 1
    gapMinute = 60 - myMinute + minute
  }
  var gapHour = hour - myHour
  if (gapHour < 0) {
    day -= 1
    gapHour = 24 - myHour + hour
  }
  var gapDay = day - myDay
  if (gapDay < 0) {
    month -= 1
    gapDay = getDaysOfMonth(time) - myDay + day
  }
  var gapMonth = month - myMonth
  if (gapMonth < 0) {
    year -= 1
    gapMonth = 12 - myMonth + month
  }
  var gapYear = year - myYear
  if (gapYear < 0) gapYear = 0

  gapMonth = gapMonth < 10 ? '0' + gapMonth : gapMonth
  gapDay = gapDay < 10 ? '0' + gapDay : gapDay

  return {
    year: gapYear,
    month: gapMonth,
    day: gapDay,
  }
}

export function getTwoDateAgeStage(time, now = new Date()) {}

/**
 * 获取年龄对应的阶段
 * @param {String} year [年]
 * @param {String} month [月]
 */
export function getAgeStage(year, month) {
  var sum = year * 12 + month * 1

  if (sum > 0 && sum <= 1) return '0-1个月'
  if (sum > 1 && sum <= 4) return '1-3个月'
  if (sum > 4 && sum <= 7) return '4-6个月'
  if (sum > 7 && sum <= 10) return '7-9个月'
  if (sum >= 12 && sum < 18) return '1-1.5岁'
  if (sum >= 18 && sum < 24) return '1.5-2岁'
  if (sum >= 24 && sum < 36) return '2-3岁'
  if (sum >= 36 && sum < 48) return '3-4岁'
  if (sum >= 48 && sum < 60) return '4-5岁'
  if (sum >= 60 && sum < 72) return '5-6岁'
  return `6岁+`
}

//日期之间的相差的天数
export function getDateDuration(birthday) {
  let time = birthday
    .replace('年', '-')
    .replace('月', '-')
    .replace('日', '')

  let dateStart = new Date(time)
  let dateEnd = new Date()
  let result = (dateEnd - dateStart) / (1000 * 60 * 60 * 24)
  return Math.round(result)
}

export function getDaysOfMonth(dateStr) {
  var date = new Date(dateStr)
  var year = date.getFullYear()
  var mouth = date.getMonth() + 1
  var day = 0

  if (mouth == 2) {
    day = isLeapYear(year) ? 29 : 28
  } else if (
    mouth == 1 ||
    mouth == 3 ||
    mouth == 5 ||
    mouth == 7 ||
    mouth == 8 ||
    mouth == 10 ||
    mouth == 12
  ) {
    day = 31
  } else {
    day = 30
  }
  return day
}

export function isLeapYear(year) {
  return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

export function getSlashDate(date, code = '/') {
  let year = date.getFullYear() //获取完整的年份(4位,1970-????)
  let month = date.getMonth() + 1 //获取当前月(0-11,0代表1月,老外系统，国内加1)
  let day = date.getDate() //获取当前日(1-31)

  month = `${month}`.padStart(2, '0')
  day = `${day}`.padStart(2, '0')
  return `${year}${code}${month}${code}${day}`
}
